//
//  GYDRefreshHeader.swift
//  Weibo
//
//  Created by 贵永冬 on 16/7/29.
//  Copyright © 2016年 贵永冬. All rights reserved.
//

import UIKit
import ReactiveCocoa


class GYDRefreshHeader: MJRefreshNormalHeader {
    
    var showMusic : Bool = false
    private var musicStatus : Bool = false
    
    override func placeSubviews() {
        super.placeSubviews()
        // 箭头的中心点
        let arrowCenterX = GYDWindowWidth/2.0 - 30 - self.arrowView.image!.size.width/2.0
        let arrowCenterY = self.mj_h * 0.5
        let arrowCenter = CGPointMake(arrowCenterX, arrowCenterY)
        self.arrowView.mj_size = self.arrowView.image!.size
        self.arrowView.center = arrowCenter
        self.loadingView.center = arrowCenter
        
    }
    
    func settingParameter() {
        self.backgroundColor = BASECOLOR
        self.lastUpdatedTimeLabel.hidden = true
        self.stateLabel.font = UIFont.systemFontOfSize(13)
        self.stateLabel.textColor = RGB(150, g: 150, b: 150)
        self.arrowView.image = UIImage(named: "tableview_pull_refresh")
        self.setTitle("下拉刷新", forState: MJRefreshState.Idle)
        self.setTitle("释放刷新", forState: MJRefreshState.Pulling)
        self.setTitle("加载中...", forState: MJRefreshState.Refreshing)
        
        
        self.rac_valuesAndChangesForKeyPath("state", options: NSKeyValueObservingOptions.New, observer: nil).subscribeNext { (x) in
            if self.showMusic == false {
                return
            }
            let p = x as! RACTuple
            guard let status : Int = p.first as? Int else {
                return
            }
            if status == 2 {
                self.musicStatus = true
                playMusic("prlm_sound_pull")
            }else if status == 1 {
                if self.musicStatus == true {
                    self.musicStatus = false
                    playMusic("prlm_sound_release")
                }
            }else if status == 3 {
                self.musicStatus = false
            }
        }
    }
}
